/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package musicplayer;


import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.filechooser.FileFilter;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


/**
 *
 * @author Mr.D
 */
public class Custom extends JFrame implements MouseListener, MouseMotionListener{
    private JPanel panel;
    private int oldx, oldy;
    private boolean changeposition = true;
    private Toolkit tk;
    private Dimension screensize;
    private String usertext = "", userimagepath = "";
    private FileFilter ff;
    private JFileChooser fc;
    private JTextField tft, tfi;
    private JButton bok, bcancel, blink;
    private JLabel ltext, limage, ltitle;
    private Image bgim = null;
    public Custom(final Manager manager){
        super("Custom");
        panel = new JPanel();
        panel.addMouseListener(this);
        panel.addMouseMotionListener(this);
        tk  = Toolkit.getDefaultToolkit();
        screensize = tk.getScreenSize();
        setContentPane(panel);
        panel.setLayout(null);
        setBounds(250, 200, 250, 130);
        setAlwaysOnTop(true);
        setResizable(false);
        tft = new JTextField();
        tfi = new JTextField();
        tft.setText("☺(Có thể dùng ký tự đặt biệt)");
        tfi.setEditable(false);
        bok = new JButton("OK");
        bcancel = new JButton("Cancel");
        blink = new JButton("...");
        ltext = new JLabel("Type Text:");
        limage = new JLabel("Choose Image: ");
        ltitle = new JLabel("Chỉ áp dụng với Text và Image");
        ltitle.setBounds(45, 2, 200, 20);
        ltext.setBounds(5, 25, 100, 20);
        limage.setBounds(5, 45, 100, 20);
        tft.setBounds(100, 25, 100, 20);
        tfi.setBounds(100, 45, 100, 20);
        bok.setBounds(60, 70, 75, 20);
        bcancel.setBounds(145, 70, 75, 20);
        blink.setBounds(205, 45, 20, 20);
        panel.add(ltitle);
        panel.add(ltext);
        panel.add(limage);
        panel.add(tft);
        panel.add(tfi);
        panel.add(bok);
        panel.add(bcancel);
        panel.add(blink);
        fc = new JFileChooser();
        ff = new FileFilter() {
            public boolean accept(File pathname) {
                if (pathname.isDirectory()) {
                    return true;
                }
                else
                {
                    String ext = "";
                    String s = pathname.getName();
                    int i = s.lastIndexOf('.');

                    if (i > 0 &&  i < s.length() - 1) {
                        ext = s.substring(i+1).toLowerCase();
                    }
                    if((ext.compareTo("jpg")==0)||(ext.compareTo("png")==0))
                        return true;
                    else
                        return false;
                }
            }
            public String getDescription() {
                return "jpg, png (300x200 là vừa vặn với khung hình)";
            }
        };
        fc.setFileFilter(ff);
        tft.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                usertext = tft.getText();
                manager.setUserInfo(usertext, userimagepath);
                setVisible(false);
            }
        });
        blink.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if(fc.showOpenDialog(panel) == JFileChooser.APPROVE_OPTION){
                    if(fc.getSelectedFile().isFile()&&fc.getSelectedFile().exists()){
                        userimagepath = fc.getSelectedFile().getPath();
                        tfi.setText(userimagepath);
                    }
                }
            }
        });
        bok.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                usertext = tft.getText();
                manager.setUserInfo(usertext, userimagepath);
                setVisible(false);
            }
        });
        bcancel.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                setVisible(false);
            }
        });
    }

    public void mouseClicked(MouseEvent e) {
        
    }

    public void mousePressed(MouseEvent e) {
        oldx = e.getX();
        oldy = e.getY();        
    }

    public void mouseReleased(MouseEvent e) {
        
    }

    public void mouseEntered(MouseEvent e) {
        
    }

    public void mouseExited(MouseEvent e) {
        
    }

    public void mouseDragged(MouseEvent e) {
        int newx = getX();
        int newy = getY();
        if(changeposition){
            if((getX() == 0)&&((e.getX()- oldx > 20)||(e.getX() - oldx < -20)))
                setLocation(e.getXOnScreen() - oldx, newy);
            if((getX() == (screensize.width - getWidth()))&&((oldx - e.getX() > 20)||(oldx - e.getX() < -20)))
                setLocation(oldx - e.getXOnScreen(), newy);
            if((getX() < 20)&&(getX() > - 20)){
                setLocation(0, newy);
            }
            else if((getX()+ getWidth() > screensize.width - 20)&&((getX()+ getWidth() < screensize.width + 20)))
                setLocation(screensize.width - getWidth(), newy);
            else{
                    setLocation(e.getXOnScreen() - oldx, newy);
            }
//            setEnabled(false);
//            setEnabled(true);
//            setVisible(false);
//            setVisible(true);
            repaint();
            changeposition = false;
        }
        else{
            if((getY() == 0)&&((e.getY()- oldy > 20)||(e.getY() - oldy < -20))){
                    setLocation(newx, e.getYOnScreen() - oldy);
            }
            if((getY() == (screensize.height - getHeight()))&&((oldy - e.getY() > 20)||(oldy - e.getY() < -20)))
                setLocation(newx, oldy - e.getYOnScreen());
            if((getY() < 20)&&(getY() > - 20)){
                    setLocation(newx, 0);
            }
            else if((getY()+ getHeight() > screensize.height - 20)&&((getY() + getHeight() < screensize.height + 20)))
                setLocation(newx, screensize.height - getHeight());
            else{
                    setLocation(newx, e.getYOnScreen() - oldy);
            }
//            setEnabled(false);
//            setEnabled(true);
//            setVisible(false);
//            setVisible(true);
            repaint();
            changeposition = true;
        }
    }

    public void mouseMoved(MouseEvent e) {
        
    }
    public void setTexts(String text, String path){
        this.usertext = text;
        this.userimagepath = path;
        tft.setText(usertext);
        tfi.setText(userimagepath);
    }
}
